<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="closeDialog"
    @open="openDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds" v-loading="isLoading">
      <!-- <el-checkbox label="110">管理员</el-checkbox>
      <el-checkbox label="113">开发者</el-checkbox>
      <el-checkbox label="115">人事</el-checkbox> -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="clickSubmit"
        >确定</el-button
      >
      <el-button size="small" @click="closeDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getBaseUserInfoApi } from '@/api/user'
import { assignRolesApi } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: [],
      isLoading: false
    }
  },
  // created() {
  //   this.getRoleList()
  // },

  methods: {
    // 点击确定,发送请求
    async clickSubmit() {
      await assignRolesApi({ id: this.userId, roleIds: this.roleIds })
      this.$message.success('分配角色成功')
      this.$emit('update:showRoleDialog', false)
    },
    // 打开弹框
    // async openDialog() {
    //   this.isLoading = true
    //   await this.getRoleList()
    //   await this.getBaseUserInfo()
    //   this.isLoading = false
    // },
    openDialog() {
      this.isLoading = true
      Promise.all([this.getRoleList(), this.getBaseUserInfo()]).then(() => {
        this.isLoading = false
      })
    },
    // 获取角色列表
    async getRoleList() {
      const { data } = await getRoleListApi(1, 9999)
      // console.log(data)
      this.roleList = data.rows
    },
    // 回显用户已有的角色
    async getBaseUserInfo() {
      const { data } = await getBaseUserInfoApi(this.userId)
      // console.log(data)
      this.roleIds = data.roleIds
    },
    // 关闭弹框
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>
